# 用cmd执行编译，不在项目中或powershell中执行编译命令

cmake_minimum_required(VERSION 4.0.3)
project(WasmModule LANGUAGES CXX)

# 设置EMSCRIPTEN工具链
set(CMAKE_TOOLCHAIN_FILE ${EMSDK}/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake)

# 收集所有cpp文件
file(GLOB SOURCES "*.cpp")

# 输出目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../../resources/wasm)

# 编译选项（仅优化）
add_compile_options(-O3)

# 生成可执行目标
add_executable(cpp_mod ${SOURCES})

# 链接选项（WASM相关参数）
target_link_options(cpp_mod PRIVATE
  "-s WASM=1"
  "-s DISABLE_EXCEPTION_CATCHING=0"
  "-s MODULARIZE=1"
  "-s EXPORT_NAME='createModule'"
  "-s EXPORTED_FUNCTIONS='[${EXPORTED_FUNCS}]'"
  "-s ALLOW_TABLE_GROWTH=1"  # 支持动态函数表扩展
)

# 设置输出后缀
set_target_properties(cpp_mod PROPERTIES SUFFIX ".js")
